home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-06  |  11.8 KB  |  490 lines

  1. /* Hash tables.
  2.    Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: Not in FSF. */
  21.  
  22. #ifdef emacs
  23. #include <config.h>
  24. #include "lisp.h"
  25.  
  26. #define NULL_ENTRY (LISP_TO_VOID (Qnil))
  27.  
  28. #else /* !emacs */
  29.  
  30. #define NULL_ENTRY ((void *) 1)
  31.  
  32. #endif /* !emacs */
  33.  
  34. #include <string.h>
  35. #include "hash.h"
  36. #include "elhash.h"
  37.  
  38. static CONST int 
  39. primes []={
  40.   13,
  41.   29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 
  42.   761, 919, 1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 
  43.   8419, 10103, 12143, 14591, 17519, 21023, 25229, 30293, 36353, 43627, 52361, 
  44.   62851, 75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449, 
  45.   389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319,
  46.   2009191, 2411033, 2893249
  47. };
  48.  
  49. /* strings code */
  50.  
  51. /* from base/generic-hash.cc, and hence from Dragon book, p436 */
  52. unsigned long
  53. string_hash (CONST void *xv)
  54.   unsigned int h = 0;
  55.   unsigned int g;
  56.   unsigned CONST char *x = (unsigned CONST char *) xv;
  57.  
  58.   if (!x) return 0;
  59.  
  60.   while (*x != 0)
  61.     {
  62.       h = (h << 4) + *x++;
  63.       if ((g = h & 0xf0000000) != 0)
  64.     h = (h ^ (g >> 24)) ^ g;
  65.     }
  66.  
  67.   return h;
  68. }
  69.  
  70. unsigned long
  71. memory_hash (CONST void *xv, int size)
  72.   unsigned int h = 0;
  73.   unsigned int g;
  74.   unsigned CONST char *x = (unsigned CONST char *) xv;
  75.  
  76.   if (!x) return 0;
  77.  
  78.   while (size > 0)
  79.     {
  80.       h = (h << 4) + *x++;
  81.       if ((g = h & 0xf0000000) != 0)
  82.     h = (h ^ (g >> 24)) ^ g;
  83.       size--;
  84.     }
  85.  
  86.   return h;
  87. }
  88.  
  89. static int 
  90. string_eq (CONST void *st1v, CONST void *st2v)
  91. {
  92.   CONST char *st1 = (CONST char *)st1v;
  93.   CONST char *st2 = (CONST char *)st2v;
  94.  
  95.   if (!st1)
  96.     return (st2)?0:1;
  97.   else if (!st2)
  98.     return 0;
  99.   else
  100.     return !strcmp (st1, st2);
  101. }
  102.  
  103.  
  104. static unsigned int 
  105. prime_size (unsigned int size)
  106. {
  107.   unsigned int i;
  108.   CONST int lim = countof (primes);
  109.   for (i = 0; i < lim; i++)
  110.     if (size <= primes [i]) return primes [i];
  111.   return primes [lim - 1];
  112. }
  113.  
  114. static void rehash (hentry *harray, c_hashtable ht, unsigned int size);
  115.  
  116. #define KEYS_DIFFER_P(old, new, testfun) \
  117.   ((testfun)?(((old) == (new))?0:(!(testfun ((old), new)))):((old) != (new)))
  118.  
  119. CONST void *
  120. gethash (CONST void *key, c_hashtable hash, CONST void **ret_value)
  121. {
  122.   hentry *harray = hash->harray;
  123.   int (*test_function)() = hash->test_function;
  124.   unsigned int hsize = hash->size;
  125.   unsigned int hcode_initial = 
  126.     (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
  127.   unsigned int hcode = hcode_initial % hsize;
  128.   hentry *e = &harray [hcode];
  129.   CONST void *e_key = e->key;
  130.  
  131.   if (!key) 
  132.     {
  133.       *ret_value = hash->zero_entry;
  134.       return (void *) hash->zero_set;
  135.     }
  136.     
  137.   if ((e_key)?
  138.       (KEYS_DIFFER_P (e_key, key, test_function)):
  139.       (e->contents == NULL_ENTRY))
  140.     {
  141.       unsigned int h2 = hsize - 2;
  142.       unsigned int incr = 1 + (hcode_initial % h2);
  143.       do
  144.         {
  145.           hcode = hcode + incr;
  146.           if (hcode >= hsize) hcode = hcode - hsize;
  147.           e = &harray [hcode];
  148.           e_key = e->key;
  149.         } 
  150.       while ((e_key)?
  151.              (KEYS_DIFFER_P (e_key, key, test_function)):
  152.              (e->contents == NULL_ENTRY));
  153.     }
  154.  
  155.   *ret_value = e->contents;
  156.   return e->key;
  157. }
  158.  
  159. void 
  160. clrhash (c_hashtable hash)
  161. {
  162.   memset (hash->harray, 0, sizeof (hentry) * hash->size);
  163.   hash->zero_entry = 0;
  164.   hash->zero_set = 0;
  165.   hash->fullness = 0;
  166. }
  167.  
  168. void
  169. free_hashtable (c_hashtable hash)
  170. {
  171. #ifdef emacs
  172.   if (!NILP (hash->elisp_table))
  173.     return;
  174. #endif
  175.   xfree (hash->harray);
  176.   xfree (hash);
  177. }
  178.  
  179. c_hashtable
  180. make_hashtable (unsigned int hsize)
  181. {
  182.   c_hashtable res = (c_hashtable) xmalloc (sizeof (struct _C_hashtable));
  183.   memset (res, 0, sizeof (struct _C_hashtable));
  184.   res->size = prime_size ((13 * hsize) / 10);
  185.   res->harray = (hentry *) xmalloc (sizeof (hentry) * res->size);
  186. #ifdef emacs
  187.   res->elisp_table = Qnil;
  188. #endif
  189.   clrhash (res);
  190.   return res;
  191. }
  192.  
  193. c_hashtable
  194. make_general_hashtable (unsigned int hsize,
  195.             unsigned long (*hash_function) (CONST void *),
  196.             int (*test_function) (CONST void *, CONST void *))
  197. {
  198.   c_hashtable res = (c_hashtable) xmalloc (sizeof (struct _C_hashtable));
  199.   memset (res, 0, sizeof (struct _C_hashtable));
  200.   res->size = prime_size ((13 * hsize) / 10);
  201.   res->harray = (hentry *) xmalloc (sizeof (hentry) * res->size);
  202.   res->hash_function = hash_function;
  203.   res->test_function = test_function;
  204. #ifdef emacs
  205.   res->elisp_table = Qnil;
  206. #endif
  207.   clrhash (res);
  208.   return res;
  209. }
  210.  
  211. c_hashtable 
  212. make_strings_hashtable (unsigned int hsize)
  213. {
  214.   return make_general_hashtable (hsize, string_hash, string_eq);
  215. }
  216.  
  217. #ifdef emacs
  218. unsigned int
  219. compute_harray_size (unsigned int hsize)
  220. {
  221.   return prime_size ((13 * hsize) / 10);
  222. }
  223. #endif
  224.  
  225. void
  226. copy_hash (c_hashtable dest, c_hashtable src)
  227. {
  228. #ifdef emacs
  229.   /* if these are not the same, then we are losing here */
  230.   if ((NILP (dest->elisp_table)) != (NILP (src->elisp_table)))
  231.     {
  232.       error ("Incompatible hashtable types to copy_hash.");
  233.       return;
  234.     }
  235. #endif
  236.  
  237.   if (dest->size != src->size)
  238.     {
  239. #ifdef emacs
  240.       if (!NILP (dest->elisp_table))
  241.         elisp_hvector_free (dest->harray, dest->elisp_table);
  242.       else
  243. #endif
  244.         xfree (dest->harray);
  245.  
  246.       dest->size = src->size;
  247. #ifdef emacs
  248.       if (!NILP (dest->elisp_table))
  249.         dest->harray = 
  250.           (hentry *) elisp_hvector_malloc
  251.             (sizeof (hentry) * dest->size, dest->elisp_table);
  252.       else
  253. #endif
  254.         dest->harray = (hentry *) xmalloc (sizeof (hentry) * dest->size);
  255.     }
  256.   dest->fullness = src->fullness;
  257.   dest->zero_entry = src->zero_entry;
  258.   dest->zero_set = src->zero_set;
  259.   dest->hash_function = src->hash_function;
  260.   dest->test_function = src->test_function;
  261.   memcpy (dest->harray, src->harray, sizeof (hentry) * dest->size);
  262. }
  263.   
  264. static void
  265. grow_hashtable (c_hashtable hash, unsigned int new_size)
  266. {
  267.   unsigned int old_hsize = hash->size;
  268.   hentry *old_harray = hash->harray;
  269.   unsigned int new_hsize = prime_size (new_size);
  270.   hentry *new_harray;
  271.  
  272. #ifdef emacs
  273.   /* We test for Qzero to facilitate free-hook.c.  That module creates
  274.      a hashtable very very early, at which point Qnil has not yet
  275.      been set and is thus all zeroes.  Qzero is "automatically"
  276.      initialized at startup because its correct value is also all
  277.      zeroes. */
  278.   if (!NILP (hash->elisp_table) && !EQ (hash->elisp_table, Qzero))
  279.     new_harray = (hentry *) elisp_hvector_malloc (sizeof (hentry) * new_hsize,
  280.                           hash->elisp_table);
  281.   else
  282. #endif
  283.     new_harray =
  284.       (hentry *) xmalloc (sizeof (hentry) * new_hsize);
  285.  
  286.   hash->size = new_hsize;
  287.   hash->harray = new_harray;
  288.  
  289.   /* do the rehash on the "grown" table */
  290.   {
  291.     long old_zero_set = hash->zero_set;
  292.     void *old_zero_entry = hash->zero_entry;
  293.     clrhash (hash);
  294.     hash->zero_set = old_zero_set;
  295.     hash->zero_entry = old_zero_entry;
  296.     rehash (old_harray, hash, old_hsize);
  297.   }
  298.  
  299. #ifdef emacs
  300.   if (!NILP (hash->elisp_table) && !EQ (hash->elisp_table, Qzero))
  301.     elisp_hvector_free (old_harray, hash->elisp_table);
  302.   else
  303. #endif
  304.     xfree (old_harray);
  305. }
  306.  
  307. void
  308. expand_hashtable (c_hashtable hash, unsigned int needed_size)
  309. {
  310.   unsigned int hsize = hash->size;
  311.   int comfortable_size = (13 * needed_size) / 10;
  312.   if (hsize < comfortable_size)
  313.     grow_hashtable (hash, comfortable_size + 1);
  314. }
  315.  
  316. void 
  317. puthash (CONST void *key, void *cont, c_hashtable hash)
  318. {
  319.   unsigned int hsize = hash->size;
  320.   int (*test_function)() = hash->test_function;
  321.   unsigned int fullness = hash->fullness;
  322.   hentry *harray;
  323.   CONST void *e_key;
  324.   hentry *e;
  325.   unsigned int hcode_initial = 
  326.     (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
  327.   unsigned int hcode;
  328.   unsigned int incr = 0;
  329.   unsigned int h2;
  330.   CONST void *oldcontents;
  331.  
  332.   if (!key) 
  333.     {
  334.       hash->zero_entry = cont;      
  335.       hash->zero_set = 1;
  336.       return;
  337.     }
  338.  
  339.   if (hsize < (1 + ((13 * fullness) / 10)))
  340.     {
  341.       grow_hashtable (hash, hsize + 1);
  342.       hsize = hash->size;
  343.       fullness = hash->fullness;
  344.     }
  345.  
  346.   harray= hash->harray;
  347.   h2 = hsize - 2;
  348.  
  349.   hcode = hcode_initial % hsize;
  350.   
  351.   e_key = harray [hcode].key;
  352.   if (e_key && (KEYS_DIFFER_P (e_key, key, test_function)))
  353.     {
  354.       h2 = hsize - 2;
  355.       incr = 1 + (hcode_initial % h2);
  356.       do
  357.         {
  358.           hcode = hcode + incr;
  359.           if (hcode >= hsize) hcode = hcode - hsize;
  360.           e_key = harray [hcode].key;
  361.         } 
  362.       while (e_key && (KEYS_DIFFER_P (e_key, key, test_function)));
  363.     }
  364.   oldcontents = harray [hcode].contents;
  365.   harray [hcode].key = key;
  366.   harray [hcode].contents = cont;
  367.   /* if the entry that we used was a deleted entry,
  368.      check for a non deleted entry of the same key,
  369.      then delete it */
  370.   if (!e_key && (oldcontents == NULL_ENTRY))
  371.     {
  372.       if (!incr) incr = 1 + ((unsigned long) key % h2);
  373.  
  374.       do
  375.         {
  376.           hcode = hcode + incr;
  377.           if (hcode >= hsize) hcode = hcode - hsize;
  378.           e = &harray [hcode];
  379.           e_key = e->key;
  380.         }
  381.       while ((e_key)?
  382.              (KEYS_DIFFER_P (e_key, key, test_function)):
  383.              (e->contents == NULL_ENTRY));
  384.  
  385.       if (e_key)
  386.         {
  387.           e->key = 0;
  388.           e->contents = NULL_ENTRY;
  389.         }
  390.     }
  391.  
  392.   /* only increment the fullness when we used up a new hentry */
  393.   if (!e_key || (KEYS_DIFFER_P (e_key, key, test_function)))
  394.     hash->fullness++;
  395. }
  396.  
  397. static void
  398. rehash (hentry *harray, c_hashtable hash, unsigned int size)
  399. {
  400.   hentry *limit = harray + size;
  401.   hentry *e;
  402.   for (e = harray; e < limit; e++)
  403.     {
  404.       if (e->key)
  405.     puthash (e->key, e->contents, hash);
  406.     }
  407. }
  408.  
  409. void 
  410. remhash (CONST void *key, c_hashtable hash)
  411. {
  412.   hentry *harray = hash->harray;
  413.   int (*test_function) (CONST void*, CONST void*) = hash->test_function;
  414.   unsigned int hsize = hash->size;
  415.   unsigned int hcode_initial = 
  416.     (hash->hash_function)?(hash->hash_function(key)):((unsigned long) key);
  417.   unsigned int hcode = hcode_initial % hsize;
  418.   hentry *e = &harray [hcode];
  419.   CONST void *e_key = e->key;
  420.  
  421.   if (!key) 
  422.     {
  423.       hash->zero_entry = 0;
  424.       hash->zero_set = 0;
  425.       return;
  426.     }
  427.  
  428.   if ((e_key)?
  429.       (KEYS_DIFFER_P (e_key, key, test_function)):
  430.       (e->contents == NULL_ENTRY))
  431.     {
  432.       unsigned int h2 = hsize - 2;
  433.       unsigned int incr = 1 + (hcode_initial % h2);
  434.       do
  435.         {
  436.           hcode = hcode + incr;
  437.           if (hcode >= hsize) hcode = hcode - hsize;
  438.           e = &harray [hcode];
  439.           e_key = e->key;
  440.         }
  441.       while ((e_key)?
  442.              (KEYS_DIFFER_P (e_key, key, test_function)):
  443.              (e->contents == NULL_ENTRY));
  444.     }
  445.   if (e_key)
  446.     {
  447.       e->key = 0;
  448.       e->contents = NULL_ENTRY;
  449.       /* Note: you can't do fullness-- here, it breaks the world. */
  450.     }
  451. }
  452.  
  453. void 
  454. maphash (maphash_function mf, c_hashtable hash, void *arg)
  455. {
  456.   hentry *e;
  457.   hentry *limit;
  458.   
  459.   if (hash->zero_set) 
  460.     ((*mf) (0, hash->zero_entry, arg));
  461.  
  462.   for (e = hash->harray, limit = e + hash->size; e < limit; e++)
  463.     {
  464.       if (e->key)
  465.     ((*mf) (e->key, e->contents, arg));
  466.     }
  467. }
  468.  
  469. void 
  470. map_remhash (remhash_predicate predicate, c_hashtable hash, void *arg)
  471. {
  472.   hentry *e;
  473.   hentry *limit;
  474.   
  475.   if (hash->zero_set && ((*predicate) (0, hash->zero_entry, arg)))
  476.     {
  477.       hash->zero_set = 0;
  478.       hash->zero_entry = 0;
  479.     }
  480.  
  481.   for (e = hash->harray, limit = e + hash->size; e < limit; e++)
  482.     if ((*predicate) (e->key, e->contents, arg))
  483.       {
  484.         e->key = 0;
  485.         e->contents = NULL_ENTRY;
  486.       }
  487. }
  488.